package studentSysterm;

import java.util.ArrayList;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        ArrayList<student> list = new ArrayList<>();

        while (true) {
            System.out.println("---------欢迎来到学生管理系统------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.查询学生");
            System.out.println("4.修改学生");
            System.out.println("5.显示学生信息");
            System.out.println("6.退出");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            loop:
            switch (choose) {
                case "1": {
                    addStudent(list);
                    break;
                }
                case "2": {
                    deleteStudent(list);
                    break;
                }
                case "3": {
                    queryStudent(list);
                    break;
                }
                case "4": {
                    modifyStudent(list);
                    break;
                }
                case "5": {
                    printStudent(list);
                    break;
                }
                case "6": {
                    System.out.println("退出");
                    //                break loop;
                    System.exit(0);
                }
                default:
                    System.out.println("没有这个选项");

            }
        }

    }
    //显示学生信息
    private static void printStudent(ArrayList<student> list) {
        if(list.size()==0){
            System.out.println("当前无学生信息");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
            student stu = list.get(i);
            System.out.println(stu.getId()+"\t\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }
    //添加学生
    public static void addStudent(ArrayList<student> list) {
        //要求：学号唯一
        System.out.println("添加学生");
        Scanner sc = new Scanner(System.in);
        student stu=new student();
        String id;
        String name;
        int age;
        String address;

        while (true) {
            System.out.println("请输入学生学号");
            id = sc.next();
            boolean checkid = checkid(list, id);
            if(checkid){
                System.out.println("id已存在，请重新输入");
            }
            else {
                stu.setId(id);
                break;
            }
        }

        System.out.println("请输入学生姓名");
        name = sc.next();
        stu.setName(name);

        System.out.println("请输入学生年龄");
        age = sc.nextInt();
        stu.setAge(age);

        System.out.println("请输入学生地址");
        address = sc.next();
        stu.setAddress(address);

        list.add(stu);
        System.out.println("添加成功！");
    }


//    删除学生
    public static void deleteStudent(ArrayList<student> list) {
        System.out.println("删除学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号");
        String id = sc.next();
        int queryid = queryid(list, id);
        if(queryid==-1){
            System.out.println("没有此学生信息,删除失败");
        }
        else{
            list.remove(queryid);
            System.out.println("id为:"+id+"删除成功");
        }
    }
    //查询学生
    public static void queryStudent(ArrayList<student> list) {
        System.out.println("查询学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号");
        String id = sc.next();
        int queryid = queryid(list, id);
        if(queryid==-1){
            System.out.println("没有此学生信息");
        }
        else{
            System.out.println("id\t\t姓名\t年龄\t地址");
            student stu = list.get(queryid);
            System.out.println(stu.getId()+"\t\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }

    }

    public static void modifyStudent(ArrayList<student> list) {
        System.out.println("修改学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号");
        String id = sc.next();
        int queryid = queryid(list, id);
        if(queryid==-1){
            System.out.println("没有此学生信息");
        }
        else{
            student stu = list.get(queryid);
            System.out.println("请输入修改后学生姓名");
            String name = sc.next();
            stu.setName(name);

            System.out.println("请输入修改后学生年龄");
            int age = sc.nextInt();
            stu.setAge(age);

            System.out.println("请输入修改后学生地址");
            String address = sc.next();
            stu.setAddress(address);

            System.out.println("学生信息修改成功");
        }

    }
    //查找学号
    private static int queryid(ArrayList<student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            String old = list.get(i).getId();
            if(old.equals(id)){
                return i;
            }
        }
        return -1;
    }
    //检查学号唯一
    private static boolean checkid(ArrayList<student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            String old = list.get(i).getId();
            if(old.equals(id)){
                return true;
            }
        }
        return false;
    }
}
